﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LanusMarket.Support;
using LanusMarket.Models;

namespace LanusMarket.Controllers
{ 
    public class PaisController : Controller
    {
        private LanusMarketContext db = new LanusMarketContext();

        public PaisController()
        {
            ViewBag.Entity = "Pais";
        }

        //
        // GET: /Paises/
        [Authorize(Roles = "admin,root")]
        public ViewResult Index()
        {
            ViewBag.Action = "Listado de";
            ViewBag.Entity = "Paises";
            this.SetTitle();
            return View(db.Paises.ToList());
        }

        //
        // GET: /Paises/Details/5
        [Authorize(Roles = "admin,root")]
        public ViewResult Details(int id)
        {
            ViewBag.Action = "Detalle de";            
            this.SetTitle();
            Pais pais = db.Paises.Find(id);
            return View(pais);
        }

        //
        // GET: /Paises/Create
        [Authorize(Roles = "admin,root")]
        public ActionResult Create()
        {
            ViewBag.Action = "Alta de";
            this.SetTitle();
            return View();
        } 

        //
        // POST: /Paises/Create
        [Authorize(Roles = "admin,root")]
        [HttpPost]
        public ActionResult Create(Pais pais)
        {
            ViewBag.Action = "Alta de";
            this.SetTitle();
            if (ModelState.IsValid)
            {
                db.Paises.Add(pais);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(pais);
        }
        
        //
        // GET: /Paises/Edit/5
        [Authorize(Roles = "admin,root")]
        public ActionResult Edit(int id)
        {
            ViewBag.Action = "Editar";
            this.SetTitle();

            Pais pais = db.Paises.Find(id);
            return View(pais);
        }

        //
        // POST: /Paises/Edit/5
        [Authorize(Roles = "admin,root")]
        [HttpPost]
        public ActionResult Edit(Pais pais)
        {            
            ViewBag.Action = "Editar";
            this.SetTitle();
            if (ModelState.IsValid)
            {
                db.Entry(pais).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(pais);
        }

        //
        // GET: /Paises/Delete/5
        [Authorize(Roles = "admin,root")]
        public ActionResult Delete(int id)
        {
            ViewBag.Action = "Eliminar";
            this.SetTitle();
            Pais pais = db.Paises.Find(id);
            return View(pais);
        }

        //
        // POST: /Paises/Delete/5
        [Authorize(Roles = "admin,root")]
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            ViewBag.Action = "Eliminar";
            this.SetTitle();

            Pais pais = db.Paises.Find(id);
            db.Paises.Remove(pais);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);            
        }
    }
}